<?php

namespace app\admin\controller;

use think\Session;

class Admin extends Action

{

    public function index()

    {

        if(input("act")=="lists"){

            $pagesize = input('pagesize');

            $pages = input('pages');

            if($pages<=1)

                $a = 0;

            else

                $a = $pagesize*($pages-1);

            $total = db('guanli')->alias('a')->where("a.ma_n","<>",'clin')->join("group b","a.ma_l=b.id","left")->count();

            $totalpage = ceil($total/$pagesize);

            $data = db('guanli')->alias('a')->where("a.ma_n","<>",'clin')->join("group b","a.ma_l=b.id","left")->order("a.id desc")->field("a.*,b.name as lname")->limit($a,$pagesize)->select();

            //输出Json数据

            return json(['code'=>1,'message'=>"获取数据成功",'data'=>$data,'totalpage'=>$totalpage]);

        }

        return view();

    }

    public function add()

    {

        

        if(request()->isPost()){

            $data["ma_n"] = input("name");
            $data["ma_p"] = md5(input("pass"));
            $data["ma_l"] = input("lever");
            $data["ma_i"] = getIP();
            $data["ma_d"] = date('Y-m-d H:i:s');
            if(request()->file('imgpath')){
                $data["imgpath"] = model("Upload")->aUpload(request()->file('imgpath'));
            }

            $save = db("guanli")->insert($data);

            if($save){

               return backUrl();

            }else{

               echo "<script>alert('当前管理员已经存在啦！');</script>";

               return backUrl();

            }

        }
        $groupRes = db("group")->field('id,name')->select();
        return $this->fetch('add',["groupRes"=>$groupRes]);

    }

    public function edit()

    {        

        $id = input("id");        

        if(request()->isPost()){

            $data["ma_n"] = input("name");
            $data["ma_l"] = input("lever");
            $data["ma_i"] = getIP();
            $data["ma_d"] = date('Y-m-d H:i:s');
            if(request()->file('imgpath')){
                if(is_file("./".input('imgtext'))) @unlink("./".input('imgtext'));
                $data["imgpath"] = model("Upload")->aUpload(request()->file('imgpath'));
            }else{
                $data["imgpath"] = input('imgtext');  
            }

            $update = db("guanli")->where("id",$id)->update($data);

            if($update!==false){
              return backUrl();
            }

        }

        $data = db("guanli")->where("id",$id)->find();
        $groupRes = db("group")->field('id,name')->select();

        $this->assign([
            "groupRes"=>$groupRes,
            'data'=>$data,
        ]);

        return view();

    }

    public function delete(){

        $del = db("guanli")->where("id","=",input("id"))->delete();       

        if($del){
            return 1;

        }else{

            return 0;

        }

    }

    public function Mypass(){

        if(request()->isPost()){

           $name = input("name");

           $pass1 = md5(input("pass1"));

           $data['ma_p'] = md5(input("pass2"));

           $update = db("guanli")->where("ma_n","=",$name)->where("ma_p","=",$pass1)->update($data);

           if($update){

               Session::delete('UserName');

               Session::delete('UserId');

               return 1;

           }else{

               return 0;

           }

        }

       return $this->fetch('pass',['name'=>session('UserName')]);

    }

}